package my.jbf.firstGame.GAMEDATA;
import android.content.*;
import android.app.*;
import android.util.*;
import android.graphics.*;


public class SharedPreferenceManager
{
	private SharedPreferences gameSP;
	private Context mContext;
	private SharedPreferences.Editor mEditor;
	public SharedPreferenceManager(Context context)
	{


		if (mContext == null)
		{
			mContext = context;
			Log.d("jbf", "SharedPreferenceManager,create context");
		}
		if (gameSP == null)
		{
			gameSP = mContext.getSharedPreferences("data", Activity.MODE_PRIVATE);
			Log.d("jbf", "SharedPreferenceManager,create gamesp");
		}
		if (mEditor == null)
		{
			mEditor = gameSP.edit();
		}
		if (gameSP.getInt("init", 0) != 1)
		{
			init();
			mEditor = gameSP.edit();
			mEditor.putInt("init", 1);
			mEditor.apply();
		}

	}
	private void init()
	{
		Log.d("jbf", "SharedPreferenceManager,init");
		resetWindowSize();//屏幕大小重置
		resetGameFrame();//屏幕刷新率重置
		resetAttackKey();//重置攻击键
		resetSmallMap();//重置小地图
		resetDirectionKey();//重置方向键
		resetAutoPlayKey();//重置自动攻击
		resetColor();//重置游戏界面各种颜色

	}
//重置游戏界面各种颜色
	public void resetColor()
	{//重置游戏各种颜色设置
		int Color_BackGround=Color.WHITE;
		int Color_DirectionKey=Color.BLACK;
		int Color_AttackKey=Color.BLACK;
		int Color_SmallMap=Color.BLACK;
		int Color_AutoPlayKey=Color.BLACK;
		mEditor.putInt("Color_BackGround",Color_BackGround);
		mEditor.putInt("Color_DirectionKey",Color_DirectionKey);
		mEditor.putInt("Color_AttackKey",Color_AttackKey);
		mEditor.putInt("Color_SmallMap",Color_SmallMap);
		mEditor.putInt("Color_AutoPlayKey",Color_AutoPlayKey);
		mEditor.apply() ;
	}
public void setBackGroundColor(int color)
{//重置背景颜色
	mEditor.putInt("Color_BackGround",color);
	mEditor.apply() ;
}
	public void setDirectionKeyColor(int color)
	{//重置方向键颜色
		mEditor.putInt("Color_DirectionKey",color);
		mEditor.apply() ;
	}
	public void setAttackKeyColor(int color)
	{//重置攻击键颜色
		mEditor.putInt("Color_AttackKey",color);
		mEditor.apply() ;
	}
	public void setSmallMapColor(int color)
	{//重置小地图颜色
		mEditor.putInt("Color_SmallMap",color);
		mEditor.apply() ;
	}
	public void setAutoPlayKeyColor(int color)
	{//重置自动游戏按键颜色
		mEditor.putInt("Color_AutoPlayKey",color);
		mEditor.apply() ;
	}


	public int getBackGroundColor()
	{//获取游戏背景颜色

		return gameSP.getInt("Color_BackGround",Color.WHITE);
	}
	public int getDirectionKeyColor()
	{//获取方向键颜色

		return gameSP.getInt("Color_DirectionKey",Color.BLACK);
	}
	public int getAttackKeyColor()
	{//获取攻击键颜色
		return gameSP.getInt("Color_AttackKey",Color.BLACK);
	}
	public int getSmallMapColor()
	{//获取小地图颜色
		return gameSP.getInt("Color_SmallMap",Color.BLACK);
	}
	public int getAutoPlayKeyColor()
	{//获取自动游戏按键颜色
		return gameSP.getInt("Color_AutoPlayKey",Color.BLACK);
	}
	//重置自动游戏键大小和位置
	public void resetAutoPlayKey()
	{//重置自动游戏按键相关设置（未重置颜色？）
		int size=0,x=0,y=0;
		size = getWindowSizeW() / 128;
		x = getWindowSizeW() * 97 / 100 - size;
		y = getWindowSizeH() / 2;
		setAutoPlayKeySize(size);
		setAutoPlayKeyXY(x, y);

	}
	public void setAutoPlayKeySize(int x)
	{//设置自动游戏按键的大小
		mEditor.putInt("AutoPlayKeySize", x);
		mEditor.apply();
	}
	public void setAutoPlayKeyXY(int x, int y)
	{//设置自动游戏按键的位置
		mEditor.putInt("AutoPlayKeyX", x);
		mEditor.putInt("AutoPlayKeyY", y);
		mEditor.apply();
	}
	public int getAutoPlayKeySize()
	{//获取自动游戏按键的大小

		return gameSP.getInt("AutoPlayKeySize", -1);
	}
	public Point getAutoPlayKeyXY()
	{//获取自动游戏按键的位置
		Point pp=new Point();
		pp.set(gameSP.getInt("AutoPlayKeyX", -1), gameSP.getInt("AutoPlayKeyY", -1));
		return pp;
	}

	public void resetWindowSize()
	{//重置窗口大小
		DisplayMetrics dm=new DisplayMetrics();
		((Activity)mContext).getWindowManager().getDefaultDisplay().getMetrics(dm);
		int w=dm.widthPixels;
		int h=dm.heightPixels;
		if (w < h)
		{
			int i=0;
			i = w;
			w = h;
			h = i;
		}
		mEditor = gameSP.edit();
		mEditor.putInt("WindowSizeW", w);
		mEditor.putInt("WindowSizeH", h);
		mEditor.apply();
	}
	public int getWindowSizeW()
	{//获取窗口宽度
		return gameSP.getInt("WindowSizeW", -1);
	}
	public int getWindowSizeH()
	{//获取窗口高度
		return gameSP.getInt("WindowSizeH", -1);
	}
	public void setWindowSize(int w, int h)
	{//设置窗口大小
		mEditor.putInt("WindowSizeW", w);
		mEditor.putInt("WindowSizeH", h);
		mEditor.apply();
	}

	public void resetDirectionKey()
	{//重置方向键大小和位置
		int size=0,x=0,y=0;
		size = getWindowSizeH() / 2;
		x = 20;
		y = getWindowSizeH()-20-size;
		setDirectionKeySize(size);
		setDirectionKeyXY(x, y);
	}
	public void setDirectionKeySize(int x)
	{//设置方向键大小
		mEditor.putInt("DirectionKeySize", x);
		mEditor.apply();
	}
	public void setDirectionKeyXY(int x, int y)
	{//设置方向键位置
		mEditor.putInt("DirectionKeyX", x);
		mEditor.putInt("DirectionKeyY", y);
		mEditor.apply();
	}
	public int getDirectionKeySize()
	{//获取方向键大小
		return gameSP.getInt("DirectionKeySize", -1);
	}
	public Point getDirectionKeyXY()
	{//获取方向键位置
		Point pp=new Point();
		pp.set(gameSP.getInt("DirectionKeyX", -1), gameSP.getInt("DirectionKeyY", -1));
		return pp;
	}

	public void resetSmallMap()
	{//重置小地图大小和位置
		int size,x,y;
		size = getWindowSizeH() / 3;
		x = getWindowSizeW()-size - 11;
		y = 11;
		setSmallMapSize(size);
		setSmallMapXY(x, y);

	}
	public void setSmallMapSize(int x)
	{//设置小地图大小
		mEditor.putInt("SmallMapSize", x);
		mEditor.apply();
	}
	public void setSmallMapXY(int x, int y)
	{//设置小地图位置
		mEditor.putInt("SmallMapX", x);
		mEditor.putInt("SmallMapY", y);
		mEditor.apply();
	}
	public Point getSmallMapXY()
	{//获取小地图位置
		Point pp=new Point();
		pp.set(gameSP.getInt("SmallMapX",-1),gameSP.getInt("SmallMapY",-1));
		return pp;
	}
	public int getSmallMapSize()
	{//获取小地图大小
		return gameSP.getInt("SmallMapSize",-1);
	}

	public void resetAttackKey()
	{//重置攻击键大小和位置
		int size=0,x=0,y=0;
		size = getWindowSizeH() / 4;
		x = (int)(getWindowSizeW()-20-size*3/2);
		y = (int)(getWindowSizeH() - size*3/2-20);
		setAttackKeySize(size);
		setAttackKeyXY(x, y);


	}
	public void setAttackKeySize(int size)
	{//设置攻击键大小
		mEditor.putInt("AttackKeySize", size);
		mEditor.apply();
	}
	public void setAttackKeyXY(int x, int y)
	{//设置攻击键位置
		mEditor.putInt("AttackKeyX", x);
		mEditor.putInt("AttackKeyY", y);
		mEditor.apply();
	}
	public int getAttackKeySize()

	{//获取攻击键大小
		return gameSP.getInt("AttackKeySize", -1);
	}
	public Point getAttackKeyXY()
	{//获取攻击键位置
		Point pp=new Point();
		pp.set(gameSP.getInt("AttackKeyX", -1), gameSP.getInt("AttackKeyY", -1));
		return pp;
	}

	public void resetGameFrame()
	{//重置屏幕刷新率
		mEditor = gameSP.edit();
		mEditor.putInt("GameFrame", 30);
		mEditor.apply();
	}
	public void setGameFrame(int x)
	{//设置屏幕刷新率
		mEditor.putInt("GameFrame", x);
		mEditor.apply();
	}
	public int getGameFrame()

	{//获取屏幕刷新率
		return gameSP.getInt("GameFrame", 30);
	}
	public void setUserName(String name)
	{//设置登录界面自动保存的用户名
		mEditor.putString("UserName",name);
		mEditor.apply();
	}
	public String getUserName()
	{//获取登录界面自动保存的用户名
		return gameSP.getString("UserName","");
	}
	public void setUserID(String id)
	{//设置游戏用用户识别ID
		
			mEditor.putString("UserID",id);
			mEditor.apply();
		}
	public String getUserID()
	{//获取游戏用用户识别ID
		return gameSP.getString("UserID","");
	}
	public void setUserPSW(String PSW)
	{//设置用户密码
		mEditor.putString("UserPSW",PSW);
		mEditor.apply();
	}
	public String getUserPSW()
	{//获取用户密码
		return gameSP.getString("UserPSW","");
	}
	public void setAutoLoginCheckButton(boolean b)
	{//设置“自动登录”多选框状态

		mEditor.putBoolean("AutoLoginCheckButtonType",b);
		mEditor.apply();
	}
	public boolean getAutoLoginCheckButton()
	{//获取“自动登录”多选框状态
		return gameSP.getBoolean("AutoLoginCheckButtonType",false);
	}
	
	public void setSavePSWCheckButton(boolean b)
	{//设置“保存密码”多选框状态
	
		
		mEditor.putBoolean("SavePSWCheckButtonType",b);
		mEditor.apply();
	}
	public boolean getSavePSWCheckButton()
	{//获取“保存密码”多选框状态
		return gameSP.getBoolean("SavePSWCheckButtonType",false);
	}
	
	public void setNetRadioButton()
	{//变更“网络”单选框状态

		boolean i = !getNetRadioButton();
		mEditor.putBoolean("NetRadioButtonType",i);
		mEditor.apply();
		
		
	}
	public boolean getNetRadioButton()
	{//获取“网络”单选框状态
		return gameSP.getBoolean("NetRadioButtonType",false);
	}
	public void setAutoStartType()
	{//设置“自动登录游戏”状态
		mEditor.putBoolean("AutoStartType",!getAutoStartType());
		mEditor.apply();
	}
	public boolean getAutoStartType()
	{//获取“自动登录游戏”状态
		return gameSP.getBoolean("AutoStartType",false);
	}
} 
